<template>
  <el-button type="primary" @click="addFn">添加用户</el-button>
  <el-table :data="list" stripe style="width: 100%" >
    <el-table-column type="index" label="序号" width="100px" />
    <el-table-column prop="username" label="账号" />
    <el-table-column fixed="right" label="操作" width="200px">
      <template #default="scope">
        <el-button link type="primary" @click="editFn(scope.row)">修改密码</el-button>
        <el-button link type="warning" @click="delFn(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog title="添加" v-model="isShow" width="30%" draggable>
    <el-form :model="formData" label-position="top">
      <el-form-item label="用户名">
        <el-input :disabled="isEdit" v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="formData.password"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="addOrEditFn">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs } from 'vue'
import { InitData } from '@/types/user'
import { getUser, addUser, editUser, delUser, gptApi } from '@/http/api'
import { ElMessage, ElMessageBox } from 'element-plus'

export default defineComponent({
  setup() {
    const gptFn = async () => {
      const res = await gptApi('介绍一下你自己')
      console.log(res);
    }

    const data = reactive(new InitData())

    const getUserList = async () => {
      const res = await getUser()
      // console.log(res);
      data.list = res.data
      // console.log(data.list);
    }
    getUserList()

    const editFn = (row: any) => {
      data.isEdit = true
      data.isShow = true
      // console.log(row)
      data.formData = {
        username: row.username,
        password: ''
      }
      data.id = row.id
    }

    const addFn = () => {
      data.formData = {
        username: '',
        password: ''
      }
      data.isShow = true
      data.isEdit = false
    }

    const addOrEditFn = async () => {
      if (data.isEdit) {
        // 修改
        const res1 = await editUser(data.formData, data.id)
        if (res1.code === 20000) {
          ElMessage({
            type: 'success',
            message: '修改成功！'
          })
        } else {
          ElMessage({
            type: 'warning',
            message: '修改失败！'
          })
        }
      } else {
        // 添加
        const res2 = await addUser(data.formData)
        if (res2.code === 20000) {
          ElMessage({
            type: 'success',
            message: '添加成功！'
          })
        } else {
          ElMessage({
            type: 'warning',
            message: '添加失败！'
          })
        }
      }
      data.isShow = false
      getUserList()
    }

    const delFn = (id: number) => {
      ElMessageBox.confirm('此操作将永久删除该账户，是否继续？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delUser(id)
          if (res.code === 20000) {
            ElMessage({
              type: 'success',
              message: '删除成功！'
            })
            getUserList()
          } else {
            ElMessage({
              type: 'warning',
              message: '删除失败！'
            })
          }
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '已取消删除操作！'
          })
        })
    }

    return {
      ...toRefs(data),
      getUserList,
      editFn,
      addFn,
      addOrEditFn,
      delFn,
      gptFn
    }
  }
})
</script>

<style scoped>
</style>